iT邦幫忙

2022 iThome 鐵人賽

DAY 26
0
Software Development

戀戀 Elixir系列 第 26

二十六. List Comprehensions

  • 分享至 

  • xImage
  •  

Your work at the online fashion boutique store continues. You come up with the idea for a website feature where an outfit is suggested to the user.

While you want to give lots of suggestions, you don't want to give bad suggestions, so you decide to use a list comprehension since you can easily generate outfit combinations, then filter them by some criteria.

Clothing items are stored as a map:

%{
  item_name: "Descriptive Name",
  price: 99.00,
  base_color: "red"
}

1. Suggest a combination

Implement get_combinations/3 to take a list of tops, a list of bottoms, and keyword list of options. For now, set options to default to an empty keyword list. The function should return the cartesian product of the lists.

2. Filter out clashing outfits

Each piece of clothing has a :base_color field, use this field to filter out all combinations where the top and the bottom have the same base color.

3. Filter by combination price

Each piece of clothing has a :price field associated with it. While you want to give lots of suggestions, you want to be able to provide users an opportunity to select a price within their budget. From the keyword list of options, use :maximum_price to filter out combinations where the price of the top and bottom exceed the maximum price.

If no maximum_price is specified, the default should be 100.00


https://exercism.org/tracks/elixir/exercises/boutique-suggestions

defmodule BoutiqueSuggestions do
  @moduledoc """
  practice list comprehensions
  """

  def get_combinations(tops, bottoms, options \\ []) do
    value = Keyword.get(options, :maximum_price, 100)

    for top_item <- tops,
        bottom_item <- bottoms,
        bottom_item.base_color !== top_item.base_color and
          top_item.price + bottom_item.price <= value,
        do: {top_item, bottom_item}
  end
end

上一篇
二十五. Structs
下一篇
二十七. Agent
系列文
戀戀 Elixir30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言